import 'package:flutter/material.dart';
import 'package:flutter_demo/utils/TokenManager.dart';
import 'package:flutter_demo/utils/Validation.dart';
import 'package:flutter_demo/utils/net.dart';
import 'package:fluttertoast/fluttertoast.dart';

class LoginPage extends StatefulWidget {
  const LoginPage({super.key});

  @override
  State<LoginPage> createState() => _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  TextEditingController accountTextEdit = TextEditingController();
  TextEditingController passwordTextEdit = TextEditingController();
  bool isVisiable = false;
  bool isShowClear = false;

  @override
  void initState() {
    super.initState();
  }

  login() async {
    // dongxiaofei 12345678
    String account = accountTextEdit.text;
    String password = passwordTextEdit.text;
    if (!Validation.validateAccount(account)) return;
    if (!Validation.validatePassword(password)) return;
    Map<String, dynamic> param = {};
    param['account'] = account;
    param['password'] = password;
    dynamic result = await http.post('/driver/login/account', param);
    if (result['code'] == 200) {
      String token = result['data'];
      await TokenManager().setToken(token);
      Navigator.pushNamedAndRemoveUntil(
        context,
        '/homePage',
        (Route<dynamic> route) => false,
      );
    } else {
      Fluttertoast.showToast(msg: '登录失败[]');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        centerTitle: true,
        title: Text('神领快递', textAlign: TextAlign.center),
      ),
      body: Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.start,
          children: <Widget>[
            SizedBox(height: 40),
            Padding(
              padding: EdgeInsets.all(10),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('账号登录', style: TextStyle(fontSize: 30)),
                  Expanded(child: Spacer()),
                  Text('手机号登录 >', style: TextStyle(color: Colors.red)),
                ],
              ),
            ),
            Padding(
              padding: EdgeInsets.all(10),
              child: TextField(controller: accountTextEdit),
            ),
            Padding(
              padding: EdgeInsets.all(10),
              child: Row(
                children: [
                  Expanded(
                    child: TextField(
                      controller: passwordTextEdit,
                      obscureText: !isVisiable,
                      onChanged: (value) {
                        setState(() {
                          isShowClear = value.isNotEmpty;
                        });
                      },
                    ),
                  ),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        passwordTextEdit.clear();
                        isShowClear = false;
                      });
                    },
                    child: Visibility(
                      visible: isShowClear,
                      child: Image.asset(
                        'assets/images/clear_content.png',
                        width: 20,
                        height: 20,
                      ),
                    ),
                  ),
                  Visibility(visible: isShowClear, child: SizedBox(width: 10)),
                  GestureDetector(
                    onTap: () {
                      setState(() {
                        isVisiable = !isVisiable;
                        Fluttertoast.showToast(msg: '是否可见$isVisiable');
                      });
                    },
                    child: isVisiable
                        ? Image.asset(
                            'assets/images/eye_show.png',
                            width: 20,
                            height: 20,
                          )
                        : Image.asset(
                            'assets/images/eye_not_show.png',
                            width: 20,
                            height: 20,
                          ),
                  ),
                ],
              ),
            ),
            Row(
              children: [
                Padding(
                  padding: EdgeInsets.all(10),
                  child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: const Color.fromARGB(255, 216, 33, 20),
                      minimumSize: Size(
                        (MediaQuery.of(context).size.width - 20),
                        50,
                      ),
                    ),
                    onPressed: () {
                      login();
                    },
                    child: Expanded(
                      child: Text('登录', style: TextStyle(color: Colors.white)),
                    ),
                  ),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}
